package controller;

import inventory.Inventory;
import item.TakeableItem;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import map.GameWorld;
import model.Observer;
import model.Subject;
import view.overlay.ShopkeepingOverlay;
import entity.Avatar;
import entity.Entity;

public class ShopkeepingController extends Controller implements Observer {

	
	private ShopkeepingOverlay mOverlay;
	
	private GameControl mToggleControl;
	
	public ShopkeepingController(ShopkeepingOverlay overlay) {
		mOverlay = overlay;
		
		mToggleControl = new GameControl(KeyEvent.VK_SLASH, "CHEAT_TOGGLE_SHOPPING", "Toggle Shopping") {

			@Override
			protected void execute() {
				Avatar ava = GameWorld.getInstance().getAvatar();
				TESTshowInventories(ava, ava);
				
				toggleShopkeeping();
			}
			
		};
	}
	
	
	private void toggleShopkeeping() {
		mOverlay.toggleInventory();
	}
	
	
	
	@Override
	public void update(Subject sourceSubject) {
		// TODO Auto-generated method stub
		
	}
	
	public void TESTshowInventories(Entity e1, Entity e2) {
		
		Inventory mEntityInventory = e1.getInventory();
		Inventory e2Inv = e2.getInventory();
		
		Map<TakeableItem, MouseListener> itemMape1 = new HashMap<TakeableItem, MouseListener>();
		
		int lowerBound = 0;
		int upperBound = Math.min(mEntityInventory.getSize(), 5);
		// obtain the list, for now the first 5
		for (int i = lowerBound; i < upperBound; i++) {
			final TakeableItem item = mEntityInventory.getItem(i);
			
			if (item == null) continue;
			
			// Create a listener for it
			itemMape1.put(item, new SimpleMouseListener() {

				@Override
				public void mouseClicked(MouseEvent e) {
					
					
					// determine if we want to drop or activate
					if (e.getButton() == MouseEvent.BUTTON1) {
						// trigger this item
						sellItem(item);
					}
					
				}
				
			});
			
		}
		
		Map<TakeableItem, MouseListener> itemMape2 = new HashMap<TakeableItem, MouseListener>();
		
		lowerBound = 0;
		upperBound = Math.min(e2Inv.getSize(), 5);
		
		// obtain the list, for now the first 5
		for (int i = lowerBound; i < upperBound; i++) {
			final TakeableItem item = e2Inv.getItem(i);
			
			if (item == null) continue;
			
			// Create a listener for it
			itemMape2.put(item, new SimpleMouseListener() {

				@Override
				public void mouseClicked(MouseEvent e) {
					
					
					// determine if we want to drop or activate
					if (e.getButton() == MouseEvent.BUTTON1) {
						// trigger this item
						sellItem(item);
					}
					
				}
				
			});
			
		}
		
		
		
		
		
		
		
		// Assign the items to the overlay
		mOverlay.setInventoryItems(itemMape1, itemMape2);
		
		
	
	}
	
	private void sellItem(TakeableItem item) {
		
	}
	
	private void buyItem(TakeableItem item) {
		
	}

	@Override
	public List<GameControl> getKeyControls() {
		List<GameControl> ctrls = new ArrayList<GameControl>();
		
		
		ctrls.add(mToggleControl);
		
		
		return (ctrls);
	}
	
	@Override
	public void reset() {
		mOverlay.hideInventory();
	}

}
